package cn.senjoeson.triper.activity;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.widget.RadioGroup;

import java.util.ArrayList;

import butterknife.BindView;
import cn.senjoeson.mylibrary.utilschanged.MyToast;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.adapter.MainFragmentAdapter;
import cn.senjoeson.triper.base.BaseActivity;
import cn.senjoeson.triper.fragment.ExploreFragment;
import cn.senjoeson.triper.fragment.FavoriteFragment;
import cn.senjoeson.triper.fragment.TripFragment;

public class MainActivity extends BaseActivity {


    @BindView(R.id.viewPager)
   public   ViewPager mViewPager;
    @BindView(R.id.group)
    public   RadioGroup mGroup;


    private String[] mTtilts = new String[]{"Favorite", "Trip", "Explore"};
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new FavoriteFragment());
        fragments.add(new TripFragment());
        fragments.add(new ExploreFragment());
        MainFragmentAdapter mainFragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager());
        mainFragmentAdapter.setTitls(mTtilts);
        mainFragmentAdapter.setFragments(fragments);
        mViewPager.setAdapter(mainFragmentAdapter);


    }

    @Override
    protected void initListener() {

        mGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.favorite:
                        mViewPager.setCurrentItem(0, false);
                        break;
                    case R.id.trip:
                        mViewPager.setCurrentItem(1, false);
                        break;
                    case R.id.explore:
                        mViewPager.setCurrentItem(2, false);
                        break;
                }
            }
        });


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mGroup.check(R.id.favorite);
                        break;
                    case 1:
                        mGroup.check(R.id.trip);
                        break;
                    case 2:
                        mGroup.check(R.id.explore);
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mViewPager.setCurrentItem(1);


    }


    @Override
    protected void initData() {

    }


    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            setIsTransAnim(false);
            finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            MyToast.showToast(getResources().getString(R.string.press_again));
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction()==MotionEvent.ACTION_DOWN){

            
        }
        return super.onTouchEvent(event);
    }
}
